import { Injectable } from '@angular/core';

import swal, { SweetAlertType , SweetAlertOptions } from 'sweetalert2';

@Injectable()
export class MessageService {

    info(message: string, title?: string): any {
        return this.alertType('info', message, title); 
    }

    success(message: string, title?: string): any {
        return this.alertType('success', message, title);
    }

    warn(message: string, title?: string): any {
        return this.alertType('warning', message, title);
    }

    error(message: string, title?: string): any {
        return this.alertType('error', message, title);
    }

    confirm(message: string, callback?: (result: boolean) => void): any;
    confirm(message: string, title?: string, callback?: (result: boolean) => void): any;

    confirm(message: string, titleOrCallBack?: string | ((result: boolean) => void), callback?: (result: boolean) => void): any {
       /**/
        const opt: SweetAlertOptions = {
            type: 'warning',
            title: 'Are you sure?',
            text: message,
            showCancelButton: true,
            cancelButtonText: 'Cancel',
            confirmButtonColor: '#DD6B55',
            confirmButtonText: 'Yes'
        };

        let cb = callback;
       
        if (typeof titleOrCallBack === 'string') {
            opt.title = titleOrCallBack; //
        } else {
            cb = titleOrCallBack;
        }

        swal(opt).then((res) => {
            
            if (res.dismiss) {
                cb(false);
            }else {
                cb(true);
            }
        })
        .catch(function(){
            cb(false);
        });

    }

    alertType(type: SweetAlertType, message: string, title?: string) {
       return swal(message, title  , type);
    }

}
